package com.atguigu.eduUcenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.UcenterMemberOrder;
import com.atguigu.eduUcenter.entity.UcenterMember;
import com.atguigu.eduUcenter.entity.vo.RegisterVo;
import com.atguigu.eduUcenter.service.UcenterMemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author lhl
 * @since 2022-07-11
 */
@RestController
@RequestMapping("/eduUcenter/ucenterMember")
@CrossOrigin
@Slf4j
public class UcenterMemberController {
    @Autowired
    UcenterMemberService ucenterMemberService;
    // 创建登录接口
//    @GetMapping("login")  这里使用get提交会报错  因为如果使用RequestBody  必须使用post提交
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember ucenterMember){
        log.info("登录接口执行....");
        String token = ucenterMemberService.login(ucenterMember);
        return R.ok().data("token",token);
    }
    // 注册接口
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        ucenterMemberService.register(registerVo);
        return R.ok();
    }
    // 根据token获取登录用户id
    @GetMapping("auth/getLoginInfo")
    public R getLoginInfo(HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember userInfo = ucenterMemberService.getById(memberId);
        return R.ok().data("userInfo",userInfo);
    }
    // 根据用户id获取用户信息
    @PostMapping("getInfoUc/{id}")
    public UcenterMemberOrder getUserInfo(@PathVariable String id){
        UcenterMember byId = ucenterMemberService.getById(id);
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(byId,ucenterMemberOrder);
        return ucenterMemberOrder;
    }
    // 创建 统计某天注册人数接口
    @GetMapping("countregister/{day}")
    public R registerCount(@PathVariable String day){
        Integer count = ucenterMemberService.countRegisterByDay(day);
        return R.ok().data("countRegister",count);
    }
}

